package com.itcast.proxy;

import com.itcast.proxy.impl.BenChi;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyDemo {
    public static void main(String[] args) {
        //实例化被代理对象
        BenChi benChi = new BenChi();
        Car proxy = (Car) Proxy.newProxyInstance(benChi.getClass().getClassLoader(),
                benChi.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if (method.getName().equals("run")) {
                            System.out.println("注意安全");
                        }
                        Object result = method.invoke(benChi, args);
                        return result;
                    }
                });
        proxy.run();
    }
}
